#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Initialization                                                     #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 09/01/2006                                             #
# Last Modification: 09/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 1
#
# MANUAL: <B>Welcome to 2dx_merge</B>
#
# MANUAL: This program assists you in the managing of a 2D crystal image processing project, and will do the merging of the processed data. Help is available <A HREF="http://2dx.org/documentation/2dx-software">here</A>. Try the right mouse click on the various fields or variable names, to get more information.
#
# DISPLAY: ALAT
# DISPLAY: IBOXPHS
# DISPLAY: MergeIQMAX
# DISPLAY: MergeResolution
# DISPLAY: MergeStepSize
# DISPLAY: RESMAX
# DISPLAY: RESMIN
# DISPLAY: SYM
# DISPLAY: avrgamphsNUMBER
# DISPLAY: avrgamphsRESOL
# DISPLAY: realang
# DISPLAY: realcell
# DISPLAY: revhk
# DISPLAY: rot180
# DISPLAY: rot90
# DISPLAY: tempfac
# DISPLAY: tempkeep
# DISPLAY: zstarwin
# DISPLAY: zstarrange
# DISPLAY: Thread_Number
# DISPLAY: CS
# DISPLAY: KV
#
#$end_local_vars
#
# Static directory variables at disposition are:
# appDir_2dx
# scripts-standard_2dx
# scripts-custom_2dx
#
set bin_2dx = ""
set proc_2dx = ""
set merge_res_limit = ""
set tempkeep = ""
#
#$end_vars
#
set ccp4_setup = 'y'
set scriptname = 2dx_initialize
#
\rm -f LOGS/${scriptname}.results
#
echo Launching initialization:
source ${proc_2dx}/initialize
echo Done with initialization.
source ${proc_2dx}/2dx_merge_makedirs 
#
# The following is to make sure that for the next "Import Images", the default is correctly initialized.
set initialization_reset = "y"
set initialization_executable = "y"
echo "set initialization_reset = ${initialization_reset}" >> LOGS/${scriptname}.results
echo "set initialization_executable = ${initialization_executable}" >> LOGS/${scriptname}.results
#
echo "<<@progress: 1>>"
#
#############################################################################
echo ":: "
echo "::                            *************************"
echo "::                              Welcome to 2dx_merge"
echo "::                            *************************"
echo "::                 (activate the MANUAL button top menu for help)"
echo ":: "
#############################################################################
#
#
# This memorizes the current merge directory under the variable "olddir":
set olddir = $PWD
#
echo "::The current working directory is" ${olddir}
#
set linkok = "y"
cd ..
echo "Checking link in project root director ${PWD}:"
if ( ! -e 2dx_master.cfg ) then
  echo ":: "
  echo "::WARNING: 2dx_master.cfg in project root directory is missing."
  echo ":: "
  set linkok = "n"
else
  \ls -l 2dx_master.cfg
  if ( ! -l 2dx_master.cfg ) then
    echo ":: "
    echo "::WARNING: 2dx_master.cfg is not a link in ${PWD}."
    echo ":: "
    set linkok = "n"
  endif
endif
cd ${olddir}
#
set dirlistfile = "${olddir}/2dx_merge_dirfile.dat"
#
if ( -s ${dirlistfile} ) then
  set coutfile = 'SCRATCH/2dx_getdirectories_tmp.dat'
  \rm -f ${coutfile}
  #
  ${bin_2dx}/2dx_getdirectories.exe << eot
${olddir}/2dx_merge_dirfile.dat
${olddir}
${coutfile}
eot
  #
  cat ${coutfile} | tr "\n" " " > SCRATCH/2dx_getdirectories_tmp2.dat
  set dirlist = "`cat SCRATCH/2dx_getdirectories_tmp2.dat`"
  #
  foreach dirfile ( ${dirlist} ) 
    cd ${olddir}/..
    echo "Checking link in ${dirfile}"
    if ( -d ${dirfile} ) then
      cd ${dirfile}
      if ( ! -e 2dx_master.cfg ) then
        echo ":: "
        echo "::WARNING: 2dx_master.cfg is missing in ${PWD}."
        echo ":: "
        set linkok = "n"
      else
        \ls -l 2dx_master.cfg
        if ( ! -l 2dx_master.cfg ) then
          echo ":: "
          echo "::WARNING: 2dx_master.cfg is not a link in ${PWD}."
          echo ":: "
          set linkok = "n"
        endif
      endif
      cd ${olddir}/..
    else
      set linkok = "n"
    endif
  end
endif
#
cd ${olddir}
#
if ( ${linkok} == 'n' ) then
  echo ":: "
  echo ":: WARNING: Your project has not the correct file system structure."
  echo ":: The files 2dx_master.cfg in the tilt-range directories should be links"
  echo ":: to the one-level higher version, which should be a link to the one in the"
  echo ":: merge directory."
  echo ":: "
  echo ":: Run the Custom Script Repair Project Links to repair this."
  echo ":: "
endif
#
if ( ${merge_res_limit}x == 'x' ) then
  echo ":: "
  ${proc_2dx}/linblock "#"
  ${proc_2dx}/linblock "#"
  ${proc_2dx}/linblock "#"
  echo ":: "
  echo ":: "
  echo "::   ERROR: Parameter Database in 2dx_merge is outdated."
  echo ":: "
  echo "::   Use in 2dx_image the menu function 'Save As Project Default' to update."
  echo ":: "
  echo "::   Then quit 2dx_merge (without saving) and relaunch 2dx_merge."
  echo ":: "
  echo ":: "
  ${proc_2dx}/linblock "#"
  ${proc_2dx}/linblock "#"
endif
#
echo "<<@progress: 100>>"
#
